﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using FarseerPhysics.Factories;
using FarseerPhysics.Dynamics.Joints;
using Microsoft.Xna.Framework.Input;

namespace CaptialVices
{
    public class DrawablePhysicsObject : ICustomDrawable
    {
        private Camera camera;

        private World world;
        
        public Body Body{ get; private set; }

        public Vector2 Position
        {
            get { return CoordinateHelper.ToScreen(Body.Position); }
            set { Body.Position = CoordinateHelper.ToWorld(value); }
        }
        public AnimatedSprite sprite;

        private Vector2 size;
        public Vector2 Size
        {
            get { return CoordinateHelper.ToScreen(size); }
            set { size = CoordinateHelper.ToWorld(value); }
        }
        
        ///<summary>
        /// Creates a rectangular drawable physics object
        ///</summary>
        /// <param name="world">The farseer simulation this object should be part of</param>
        /// <param name="texture">The image that will be drawn at the place of the body</param>
        /// <param name="size">The size in pixels</param>
        /// <param name="mass">The mass in kilograms</param>
        public DrawablePhysicsObject(World world, AnimatedSprite sprite, Vector2 size, float mass)
        {
            Body = BodyFactory.CreateRectangle(world, size.X * CoordinateHelper.pixelToUnit, size.Y * CoordinateHelper.pixelToUnit, 1);
            Body.BodyType = BodyType.Dynamic;
            this.Size = size;
            this.sprite = sprite;
            this.world = world;
        }

        /// <summary>
        /// Creates a circular drawable physics object
        /// </summary>
        /// <param name="world">The farseer simulation this object should be part of</param>
        /// <param name="texture">The image that will be drawn at the place of the body</param>
        /// <param name="diameter"> The diameter in pixels</param>
        /// <param name="mass">The mass in kilograms</param>
        public DrawablePhysicsObject(World world, AnimatedSprite sprite, float diameter, float mass)
        {
            size = new Vector2(diameter, diameter);
            Body = BodyFactory.CreateCircle(world, (diameter / 2.0f) * CoordinateHelper.pixelToUnit, 1);

            Body.BodyType = BodyType.Dynamic;

            this.Size = size;
            this.sprite = sprite;
            this.world = world;
        }

        public virtual void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            Rectangle destination = new Rectangle
            (
                (int)Position.X,
                (int)Position.Y,
                (int)Size.X,
                (int)Size.Y
            );

            Vector2 origin = new Vector2(sprite.Width / 2, sprite.Height / 2);

            sprite.Draw(gameTime, spriteBatch, destination, origin, Body.Rotation, Color.White, 0.0f);
        }

        public event EventHandler<EventArgs> VisibleChanged;

        public Rectangle Border
        {
            get { return new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y); }
        }
        public bool IsSelected { get; set; }
        public bool IsMouseOver { get; set; }
        public bool Contains(Vector2 pointToCheck)
        {
            Point mouse = new Point((int)pointToCheck.X, (int)pointToCheck.Y);
            return Border.Contains(mouse);
        }

        private bool MouseInBody(Point mousePoint)
        {
            Rectangle rect = new Rectangle((int)(Position.X - Size.X / 2), (int)(Position.Y - Size.Y / 2), (int)Size.X, (int)Size.Y);
            return rect.Contains(mousePoint);
        }
    }  
}
